1.4 - C# Loop types


What is a loop and when should we use it?

A loop happens whenever our code in curly braces gets executed as many times as we specify or how much is needed.
Examples are doing something only while a condition is met (while loop), or doing something for as many times we need (for loop).
There are mainly 3 types of loops, and these are:

  • while loop
  • for loop
  • foreach loop

To make it more clear, we start with a while loop example:

While loop


protected override void OnGameStart()
{
    while (true)
    {
        // code in here will be executed and repeated infinitely
    }
}        
            

As you can see, the code inside the curly braces will get executed forever as the above condition is always true (which means we want to execute the code in the curly braces).
This loop type should almost never be use like this, as it will cause the game to freeze since we can't escape from it.
Furthermore, it's also almost never used anyway since for and foreach loops do the job better.

For loop


protected override void OnGameStart()
{
    // this loop set the player health to 50 for ten times
    for (int i = 0; i < 10; i++)
    {
        LocalPlayer.Vitals.SetHealth(50);
    }
    // after the loop is finished the code will continue from here
}        
            

For loops are used to do something as many times as we want or until a condition is reached.
In the above example, we are setting the player health to 50 ten times.

How do I read the above example?
Let's break it up in different parts to understand it.

  • Int i = 0: this is our counter starting point, this means we are starting from the number 0
  • i < 10: this is the condition we need to break to exit the loop, so in this example we can red it as "set the player health to 50 until i is < 10
  • i++: this is how much the i variable should increase each time the code in the curly braces finishes executing. In this example i++ means +1 so the i variable will increase by one each time an iteration finishes.

So summing up the above, the code example can be translate as "while i is less than 10, execute the code inside the curly braces".
This basically means we will set our player halth to 50 ten times, since i at the start of the loop has a value of 0 and is increased by one each time an iteration finishes.
When i will be equal to 10 and so the condition is broken, we will exit the for loop.

Foreach loop


                protected override void OnGameStart()
                {
                    List numbers = new() { 7, 12, 3, 5, 22 };
                
                    foreach (int number in numbers)
                    {
                        number // i'm the currently pointed number
                    }
                }        
            

Foreach loops are type of loops which are very similar to for loops. In fact, they basically does the same job.
The main difference is that they are used with Lists or Arrays variable types to simplify their looping writing.

In the above example, we are iterating through the numbers list, so every time we reach the end of ours curly braces, we will point to the next number in the list.
Translating in simple words: "in the first loop iteration number will be equal to the first number of the numbers list (7), at the second iteration number will be equal to the second number of the numbers list (12) etc.
When we reach the latest number in the numbers list (22) we will exit the loop.